﻿using IceZero.Common.Services;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceZero.Business.Business
{
    public class OnlineContainerBusiness
    {
        /// <summary>
        /// 添加在线对象
        /// </summary>
        /// <param name="area">区域名，用于区分不同的数据</param>
        /// <param name="key">键</param>
        /// <param name="data">数据</param>
        /// <param name="expireSeconds">过期秒数，默认 600秒,范围：0-36000</param>
        public string Add(string area, string key, Dictionary<string, string> data, long expireSeconds)
        {
            if (string.IsNullOrWhiteSpace(area))
            {
                throw new ArgumentException("arg area is invaild");
            }
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("arg key is invaild");
            }
            if (data == null)
            {
                throw new ArgumentException("arg data is invaild");
            }
            if (expireSeconds < 0 || expireSeconds > 36000)
            {
                throw new ArgumentException("arg expireSeconds is invaild ,the range is 0-36000 ");
            }
            var expireTime = DateTime.Now.AddSeconds(expireSeconds);
            OnlineContainer.Default.Add(area, key, data, expireTime);
            return "add online object";
        }
        /// <summary>
        /// 主动删除区域内所有对象
        /// </summary>
        /// <param name="areaName">区域名</param>
        /// <returns></returns>
        public bool Delete(string areaName)
        {
            if (string.IsNullOrWhiteSpace(areaName))
            {
                throw new ArgumentException("arg areaName is invalid");
            }

            return OnlineContainer.Default.Delete(areaName); ;
        }

        /// <summary>
        /// 删除域内某个key的对象
        /// </summary>
        /// <param name="areaName">区域名</param>
        /// <param name="key">键</param>
        /// <returns></returns>
        public bool DeleteByAreaKey(string areaName, string key)
        {
            if (string.IsNullOrWhiteSpace(areaName))
            {
                throw new ArgumentException("arg areaName is invalid");
            }
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("arg key is invalid");
            }

            return OnlineContainer.Default.Delete(areaName, key); ;
        }

        /// <summary>
        /// 获取域里面的某个键的对象
        /// </summary>
        /// <param name="areaName">区域名</param>
        /// <param name="key">键</param>
        /// 
        public OnlineObject Get(string areaName, string key)
        {
            if (string.IsNullOrWhiteSpace(areaName))
            {
                throw new ArgumentException("arg areaName is invalid");
            }
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("arg key is invalid");
            }

            return OnlineContainer.Default.Get(areaName, key); ;
        }

        /// <summary>
        /// 获取域里面的所有的对象
        /// </summary>
        /// <param name="areaName">区域名</param>
        /// <returns></returns>
        public ConcurrentDictionary<string, OnlineObject> GetAll(string areaName)
        {
            if (string.IsNullOrWhiteSpace(areaName))
            {
                throw new ArgumentException("arg areaName is invalid");
            }

            return OnlineContainer.Default.GetAll(areaName);
        }
        /// <summary>
        /// 获取域里面的所有对象总数
        /// </summary>
        /// <param name="areaName">区域名</param>
        public int GetCount(string areaName)
        {
            if (string.IsNullOrWhiteSpace(areaName))
            {
                throw new ArgumentException("arg areaName is invalid");
            }

            var data = OnlineContainer.Default.GetAll(areaName); ;
            if (data == null)
            {
                return 0;
            }
            return data.Keys.Count;
        }

        /// <summary>
        /// 获取域里面的所有对象总数
        /// </summary>
        /// <param name="areaName">区域名</param>
        /// <param name="dataKey">查询在线对象（OnlineObject）中的Data的key值</param>
        /// <param name="dataValue">查询在线对象（OnlineObject）中的Data的Value值</param>
        /// <returns></returns>
        public int GetCountWhere(string areaName, string dataKey, string dataValue)
        {
            if (string.IsNullOrWhiteSpace(areaName))
            {
                throw new ArgumentException("arg areaName is invalid");
            }

            if (string.IsNullOrWhiteSpace(dataKey))
            {
                throw new ArgumentException("arg dataKey is invalid");
            }

            if (string.IsNullOrWhiteSpace(dataValue))
            {
                throw new ArgumentException("arg dataValue is invalid");
            }
            var areaData = OnlineContainer.Default.GetAll(areaName);
            if (areaData == null)
            {
                return 0;
            }
            var onlineObjects = areaData.Values;
            return onlineObjects.Where(onlineObject =>
            {
                var onlineObjectData = onlineObject.Data;
                return onlineObjectData.ContainsKey(dataKey) && onlineObjectData[dataKey] == dataValue;
            }).Count();
        }
    }
}
